<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理" lazy>
          <el-button
            type="primary"
            size="small"
            @click="addclick"
          >+ 新增角色</el-button>
          <el-table border :data="list">
            <el-table-column label="序号" type="index" width="180px">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column label="角色名" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <div>
                  <!--
                    分配权限
                    1.创建弹框组件
                    2.权限树形数据展示
                    3.获取所有权限数据
                    4.获取该角色的权限数据并回显
                    5.修改调用接口
                      定义修改权限api
                      导入、调用
                      调用成功 提示，关闭
                   -->
                  <el-button
                    type="text"
                    @click="PermissionClick(row)"
                  >分配权限</el-button>
                  <el-button
                    type="text"
                    @click="editclick(row)"
                  >修改</el-button>
                  <el-button type="text" @click="delclick(row)">删除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <!-- current-page 默认页码
                  page-sizes-->
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 2, 3, 4, 5]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司设置" lazy>
          <Info />
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- <Add v-model="show" @getData="getData" /> -->
    <Add ref="add" :is-show="show" @close="close" @getData="getData" />
    <!-- 分配权限 -->
    <setPermission ref="setPermission" :is-show.sync="showPermission" />
  </div>
</template>
<script>
import { sysrole, sysroleDelete } from '@/api/setting'
import Add from './components/add.vue'
import setPermission from './components/setPermission.vue'
export default {
  name: '',
  components: {
    Info: () => import('./components/info.vue'),
    Add,
    setPermission
  },
  data() {
    return {
      showPermission: false,
      show: false,
      list: [],
      page: {
        page: 1,
        pagesize: 5,
        total: 100
      }
    }
  },
  computed: {},
  created() {
    this.getData()
  },
  methods: {
    // 页容量(每页显示多少条)改变
    handleSizeChange(size) {
      this.page.pagesize = size
      // 当页容量改变时，页码最好回到第一页
      // this.page.page = 1
      this.getData()
      console.log(size)
    },
    // 页码改变
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
      console.log(page)
    },
    // 获取列表数据
    async getData() {
      const res = await sysrole(this.page)
      console.log(res, 989)
      // 存储分页的总数
      this.page.total = res.data.total
      // 存储列表数据
      this.list = res.data.rows
    },
    addclick() {
      this.show = true
      // this.$refs.add.lookShow = true
      this.$refs.add.mode = 'add'
    },
    close() {
      this.show = false
    },
    // 删除角色
    delclick(row) {
      this.$confirm('确定删除该角色吗?', '提示')
        .then(async() => {
          await sysroleDelete(row.id)
          this.$message.success('删除成功')
          // this.handleCurrentChange(1)
          if (this.page.page > 1 && this.list.length === 1) {
            this.page.page--
          }
          this.getData(this.page.page)
        })
        .catch(() => {})
    },
    // 编辑角色
    editclick(row) {
      this.show = true
      this.$refs.add.form = JSON.parse(JSON.stringify(row))
      this.$refs.add.mode = 'edit'
    },
    // 分配权限
    PermissionClick(row) {
      this.showPermission = true
      this.$refs.setPermission.getRoleInfo(row)
    }
  }
}
</script>
<style lang="scss" scoped>
.setting {
  padding: 15px;
}
</style>
